package com.example.demo.controller;

import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
public class UserController {

    @Autowired
    UserService userService;

    @GetMapping("/user/{id}")
    public User getUser(@PathVariable("id") Integer id){
        User user =  userService.getUser(id);
        return user;
    }
    @ResponseBody
    @PostMapping("/login")
    public Map userLogin(@RequestParam String name, @RequestParam String password,HttpServletRequest request){
        Map dataMap = new HashMap();
        if( StringUtils.isEmpty(name) ){
            dataMap.put("status","10");
            dataMap.put("errMsg","用户名不能为空");
        }else if( StringUtils.isEmpty(password) ){
            dataMap.put("status","10");
            dataMap.put("errMsg","密码不能为空");
        }else{
            dataMap = userService.userLogin(name,password,request);
        }
        return dataMap;
    }
    @ResponseBody
    @GetMapping("/loginOut")
    public Map userLoginOut(HttpServletRequest request){
        Map dataMap = new HashMap();
        HttpSession session = request.getSession();
        session.removeAttribute("user");
        dataMap.put("message","登出成功");
        dataMap.put("status",0);
        System.out.println("i am here");
        return dataMap;
    }





    @GetMapping("/user")
    public User insertUser(User user){
        User save =  userService.insertUser(user);
        return save;
    }
    @GetMapping("/findUserPage/{page}")
    public Page<User> findUserPage(@PathVariable("page") Integer page){
        Page<User> result = userService.findUserPage(page);
        return result;
    }

}
